// pages/music-player/index.js
import {audioContent,store} from "../../store/index"
//import formatLyric from "../../utils/formatLyric"
import {getSongDetail} from "../../service/api_music"
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
      lyric:[],
      currentSong:{},
      durationTime:0,
      currentTime:0,
      screenHeight:0,
      currentValue:0,
      isChange:false,
      
    isPlaying: false,
    playingName: "pause",

    playModeIndex: 0,
    playModeName: "order",
    },

    handleSliderChange(e){
       //console.log(e.detail.value)
       //this.setData({currentTime:e.detail.value})
       audioContent.pause()
       audioContent.seek(e.detail.value)
       this.setData({currentTime:e.detail.value*1000,isChange:false})
       audioContent.play()
    },
    handleSliderChanging(e){
      //console.log(e.detail.value)
       //this.setData({currentTime:e.detail.value})
       audioContent.pause()
       audioContent.seek(e.detail.value)
       this.setData({currentTime:e.detail.value*1000,isChange:true})
       audioContent.play()
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad:async function (options) {
      this.setData({screenHeight:app.global.screenHeight-app.global.statusBarHeight-44})
      console.log(options)
     const result = await getSongDetail(options.id)
       // console.log(res)
    this.setData({currentSong:result.songs[0],durationTime:result.songs[0].dt})
     store.onState("lyric",(res)=>{
         if(!res[0]) return
         //console.log(res)
         this.setData({lyric:res})

     })
       audioContent.onCanplay(()=>{
       audioContent.play()
     })
      audioContent.onTimeUpdate(()=>{
        //console.log(audioContent.currentTime)
        if(!this.data.isChange) {
          this.setData({
            currentTime:Math.floor(audioContent.currentTime*1000)
          })
        }
       
      })
     
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
      
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})